package viewcontroller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import utils.ServiceHandler;

import model.Product;

import com.example.lastprojectbeautiplus.R;
import com.google.gson.Gson;


import adapter.ProductGridViewAdapter;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchFragment extends Fragment {
	public SearchFragment() {
	}

	SQLiteDatabase dbSearch;
	private Product[] listProduct;
	private GridView gvSearch;
	private ProductGridViewAdapter pAdapter;
	private Cursor csSearch;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_search_product,
				container, false);
		createSearchTable();
		csSearch = dbSearch.rawQuery("SELECT * FROM SearchTable", null);
		csSearch.moveToFirst();
		String searchText = csSearch.getString(csSearch
				.getColumnIndex("TextSearch"));
		try {
			searchText = URLEncoder.encode(searchText, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String urlSearch = "http://services-beautyplus.rhcloud.com/rest/SearchServices/Search?query="
				+ searchText;
		new getProductList(urlSearch).execute();
		dbSearch.execSQL("DELETE FROM SearchTable;");
		gvSearch = (GridView) rootView.findViewById(R.id.gvSearch);
		gvSearch.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), SlideMenu.class);
				intent.putExtra("FromProductList",
						listProduct[position].getProductID());
				getActivity().startActivity(intent);
			}
		});
		return rootView;
	}

	private class getProductList extends AsyncTask<Void, Void, Void> {
		private String url;
		private String responseProductList;

		public getProductList(String url) {
			// TODO Auto-generated constructor stub
			this.url = url;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceHandler sh = new ServiceHandler();
			responseProductList = sh.executeGetRequest(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Gson myGson = new Gson();
			listProduct = myGson.fromJson(responseProductList, Product[].class);
			pAdapter = new ProductGridViewAdapter(getActivity(),
					R.layout.gridview_product_item, listProduct);
			gvSearch.setAdapter(pAdapter);
		}
	}

	private void createSearchTable() {
		dbSearch = getActivity().openOrCreateDatabase("SearchTable",
				android.content.Context.MODE_PRIVATE, null);
		String sqlSearch = "CREATE TABLE IF NOT EXISTS SearchTable(TextSearch VARCHAR);";
		dbSearch.execSQL(sqlSearch);
	}

}
